#include "PlayerEvent.h"
#include "Log.h"
#include "MainCityProtection.h"

#include "GameSingleton.h"
#include "GameRegistry.hpp"

#include "Expand/Plugins/AuthPlugin.h"

#include "Blocks/ChestBlock.h"
#include "Player/PlayerData.h"
#include "Components/ComponentBody.h"
#include "Components/ComponentPlayer.h"
#include "Components/ComponentBlockEntity.h"
#include "Components/ComponentMiner.h"
#include "Components/ComponentCreature.h"
#include "Components/ComponentAttacker.h"
#include "Components/ComponentHealth.h"

#include "Network/PackageManager.h"
#include "Network/Packages/MessagePackage.h"

#include "Subsystems/SubsystemTerrain.h"
#include "Subsystems/SubsystemPlayers.h"

namespace CXHGT
{

    PlayerEvent::PlayerEvent(MainCityProtection * mainCityProtection) : m_mainCityProtection(mainCityProtection)
    {
        m_subsystemTerrain = GameSingleton::singletonPtr->getSingleton<SubsystemTerrain>();
        m_subsystemPlayers = GameSingleton::singletonPtr->getSingleton<SubsystemPlayers>();
    }

    static void sendMessage(ClientPtr client, const std::string & message)
    {
        std::shared_ptr<MessagePackage> package = std::make_shared<MessagePackage>(message);
        package->client = client;
        package->sendType = PackageSendType::TARGET;
        GameSingleton::packageManager->addSendPackageToQueue(package);
    }

    bool PlayerEvent::playerPlaceBlock(ComponentPlayer *componentPlayer, const Point3 &point, int32_t placeBlockValue)
    {
        if (m_mainCityProtection->isMainCity(point) && m_mainCityProtection->authPlugin->getLevelByGuid(componentPlayer->playerData->playerGuid) < 100)
        {
            sendMessage(componentPlayer->playerData->client, "这里是主城，你没有权限操作");
            return false;
        }
        return true;
    }

    bool PlayerEvent::playerBreakBlock(ComponentPlayer *componentPlayer, const Point3 &point, int32_t digBlockValue, int32_t toolLevel)
    {
        if (m_mainCityProtection->isMainCity(point) && m_mainCityProtection->authPlugin->getLevelByGuid(componentPlayer->playerData->playerGuid) < 100)
        {
            sendMessage(componentPlayer->playerData->client, "这里是主城，你没有权限操作");
            return false;
        }
        return true;
    }


    bool PlayerEvent::onUse(ComponentMiner *componentMiner, const Ray3& ray)
    {
        Vector3 position;
        componentMiner->componentCreature->componentBody->getPosition(position);
        ComponentPlayer *componentPlayer = componentMiner->componentPlayer;
        Point3 point = position.toPoint3();

        if (m_mainCityProtection->isMainCity(point) && m_mainCityProtection->authPlugin->getLevelByGuid(componentPlayer->playerData->playerGuid) < 100)
        {
            return false;
        }
        return true;
    }

    bool PlayerEvent::hitBody(ComponentAttacker* attacker, ComponentBody *body, const Vector3 &hitPosition, const Vector3 &hitDirection)
    {
        // ComponentPlayer *attackerPlayer = attacker->componentBody->componentPlayer;
        if (body->componentPlayer != nullptr)
        {
            // if (attackerPlayer != nullptr && m_mainCityProtection->authPlugin->getLevelByGuid(attackerPlayer->playerData->playerGuid) >= 100)
            // {
            //     return true;
            // }
            Vector3 position;
            body->getPosition(position);
            Point3 point = position.toPoint3();
            if (m_mainCityProtection->isMainCity(point))
            {
                return false;
            }
        }
        return true;
    }

    bool PlayerEvent::injureEntity(ComponentHealth *health, float amount, entt::entity attackerEntity, bool realInjure, const std::string &cause, InjureType injureType)
    {
        // 真实伤害就不保护了
        if (realInjure == false && health->componentPlayer != nullptr)
        {
            Vector3 position;
            health->componentPlayer->componentBody->getPosition(position);
            Point3 point = position.toPoint3();
            if (m_mainCityProtection->isMainCity(point))
            {
                return false;
            }
        }
        return true;
    }


    bool PlayerEvent::playerTouchSign(std::shared_ptr<PlayerData> playerData, const Point3 &point)
    {
        if (m_mainCityProtection->isMainCity(point) && m_mainCityProtection->authPlugin->getLevelByGuid(playerData->playerGuid) < 100)
        {
            sendMessage(playerData->client, "这里是主城，你没有权限操作");
            return false;
        }
        return true;
    }

    bool PlayerEvent::playerEditSign(std::shared_ptr<PlayerData> playerData, SignTextData &signTextData)
    {
        if (m_mainCityProtection->isMainCity(signTextData.point) && m_mainCityProtection->authPlugin->getLevelByGuid(playerData->playerGuid) < 100)
        {
            sendMessage(playerData->client, "这里是主城，你没有权限操作");
            return false;
        }
        return true;
    }

} // namespace BedWars
